from itsdangerous import BadData
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from apps.oauth.constants import OPENID_TOKEN_EXPRIES_TIME
from meiduo_mall import settings
import logging
logger = logging.getLogger('django')

def get_token_openid(openid):
    s = Serializer(settings.SECRET_KEY,OPENID_TOKEN_EXPRIES_TIME)
    data = {
        "openid":openid
    }
    token = s.dumps(data)
    return token.decode()


def check_openid_token(token):
    s = Serializer(secret_key=settings.SECRET_KEY,expires_in=OPENID_TOKEN_EXPRIES_TIME)

    try:
        result = s.loads(token)
    except BadData as e:
        logger.error(e)
        return None
    return result.get('openid')